// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  var name=''
  var name0 = ''
  var name1 = ''
  var openid1 = event.id.replace(event.openid, '')
  name0=await db.collection('users').where({
    _openid: event.openid
  }).get().then(res => {
    return res.data[0].randomName
  })
  name1=await db.collection('users').where({
    _openid: openid1
  }).get().then(res => {
    return res.data[0].randomName
  })
  name=name0+name1
  console.log(event)
  if (event.newMsg) {
    var res = db.collection('anonymousChatId').where({
      id: event.id
    }).update({
      data: {
        newMsg: _.inc(1),
        name:name
      }
    })
  } else {
    var res = db.collection('anonymousChatId').where({
      id: event.id
    }).update({
      data: {
        newMsg: 0,
        name:name
      }
    })
  }
  return res
}